package com.itheima.controller.system;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.system.Dept;
import com.itheima.utils.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 部门 控制层
 */
@WebServlet("/system/dept/*")
public class DeptServlet extends BaseServlet {


    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从前端获取分页属性
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        //System.out.println(pageNum);
        //获取列表
        Page<Dept> page = deptService.findAll(pageNum, pageSize);
        //配置分页信息
        PageInfo<Dept> pageInfo = new PageInfo<Dept>(page);
        //存入请求域
        req.setAttribute("page",pageInfo);
        //跳转页面
        req.getRequestDispatcher("/pages/system/dept/list.jsp").forward(req,resp);
    }

    //进入添加数据页面 回显信息 需要返回列表
    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取所有部门信息
        List<Dept> list = deptService.findAll();
        String json = mapper.writeValueAsString(list);
        //System.out.println(json);
        //响应数据
        resp.getWriter().write(json);
    }

    //保存信息
    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //封装信息
        Dept dept = BeanUtil.fillBean(req, Dept.class);
        dept.setId(UUID.randomUUID().toString().replace("-",""));
        //添加
        deptService.addDept(dept);
        //刷新页面
        list(req,resp);
    }

    //删除多条记录
    private void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");
        //System.out.println(ids);
        //删除
        boolean isDel = deptService.deleteByIds(ids);
        //System.out.println("============="+isDel);
        //响应
        resp.getWriter().write(mapper.writeValueAsString(isDel));
    }

    //通过id找到对象并打开编辑页面
    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Dept dept = deptService.findById(id);
        //获取到列表并设置到请求域
        req.setAttribute("deptList",deptService.findAll());
        //设置到请求域
        req.setAttribute("dept",dept);
        //跳转到编辑页面
        req.getRequestDispatcher("/pages/system/dept/update.jsp").forward(req,resp);
    }


    //通过id修改
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //封装用户信息
        Dept dept = BeanUtil.fillBean(req, Dept.class);
        //更新信息
        deptService.updateDept(dept);
        //刷新页面
        list(req,resp);
    }
}
